package com.yxhl.zoume.core.busticket.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.busticket.model.ZMSelectStartCityEntity;
import com.yxhl.zoume.core.busticket.ui.fragment.BusArrivalStationFragment;
import com.yxhl.zoume.core.busticket.ui.fragment.BusStartStationFragment;
import com.yxhl.zoume.di.component.base.HasComponent;
import com.yxhl.zoume.di.component.busticket.BusStationComponent;
import com.yxhl.zoume.di.component.busticket.DaggerBusStationComponent;
import com.yxhl.zoume.di.module.busticket.BusStationModule;

import java.io.Serializable;

/**
 * 巴士城市列表
 * Created by Xboy on 16/3/17.
 */
public class BusStationsListActivity extends BaseActivityWithToolBar implements HasComponent<BusStationComponent> {
    private static final String INTENT_EXTRA_PARAM = "intent_extra_param";

    private BusStationComponent component;
    private ZMSelectStartCityEntity mSelectStartCityEntity;

    /**
     * 打开车站列表页
     *
     * @param context               调用者
     * @param selectStartCityEntity 车站信息，出发车站传null，到达车站传出发这站信息
     * @return
     */
    public static Intent getCallingIntent(Context context, Serializable selectStartCityEntity) {
        Intent intent = new Intent(context, BusStationsListActivity.class);
        intent.putExtra(INTENT_EXTRA_PARAM, selectStartCityEntity);
        return intent;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bus_ticket_city_list;
    }

    @Override
    protected void initializeInjector() {
        this.component = DaggerBusStationComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(getActivityModule())
                .busStationModule(new BusStationModule())
                .build();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent();

        if (mSelectStartCityEntity == null) {
            addFragment(R.id.fragment_bus_station, BusStartStationFragment.newInstance());
        } else {
            addFragment(R.id.fragment_bus_station, BusArrivalStationFragment.newInstance(mSelectStartCityEntity));
        }
    }

    private void handleIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mSelectStartCityEntity = (ZMSelectStartCityEntity) intent.getSerializableExtra(INTENT_EXTRA_PARAM);
        }
    }


    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {

    }


    /**
     * 替换fragment
     * 目前在这里仅用于打开到达城市搜索
     *
     * @param fragment
     * @param addToBackStack
     */
    public void replaceFragment(Fragment fragment, boolean addToBackStack) {
        super.replaceFragment(R.id.fragment_bus_station, fragment, addToBackStack);
    }

    @Override
    public BusStationComponent getComponent() {
        return component;
    }
}
